<?php
namespace App\Repository;

use App\Model\Admin;

class AdminRepository extends \HyperfStore\Repository
{
    // 放入模型
    public function build()
    {
        return new Admin;
    }
    
    // 登陆
    public function login($data)
    {
        if($admin = Admin::tokenLogin($data))
        {
            // 只允许一个 token
            $admin->tokenFlush();
            
            return ['token' => $admin->tokenCreate('admin-token')];
        }
    }

    // 新增
    public function create($data)
    {
        return $this->exception(function() use($data) {
            return $this->save($data);
        });
    }
    
    // 更新
    public function update($data)
    {
        if(!$data['password'])
        {
            unset($data['password']);
        }
        
        return $this->create($data);
    }
    
    // 删除
    public function delete()
    {
        if($this->model->id > 1)
        {
            return $this->model->delete();
        }
    }
}